#coding=utf-8
import os
import shutil
import sys
import time
import math
import json
import zipfile
from subprocess import run
import subprocess
# import thread
# import thread6
sys.path.append('../..')
import FileUtils
import ArrayUtils
import platform

curPath = os.getcwd()
print(curPath)

projectPath = os.path.abspath(os.path.join(os.path.dirname(curPath), os.pardir,os.pardir))
print(projectPath)

sysstr = platform.system()
print(platform)

json = FileUtils.getJsonFileUtf8Data(os.path.join(curPath,"config.json"))


def systemRun(str):
	print("运行命令:"+str)
	return os.system(str)
	# pass

def addUmeng():
	print("########## 加入友盟 ##########")
	os.chdir(os.path.join(projectPath,"Framework"))
	systemRun("CALL a_wx_自动接入友盟.bat")
	
	pass

def splitMainCode():
	print("########## 分离代码 ##########")
	os.chdir(os.path.join(projectPath,"Framework"))
	systemRun("CALL a_wx_分离主逻辑.bat")
	pass

def syncBuilder():
	print("同步 builder 配置表")
	path = os.path.join(curPath,"wechatgame.json")
	gameConfig = FileUtils.getJsonFileUtf8Data(path)
	for key in gameConfig:
		# print("key:" + key)
		# print("json = ")
		# print(json)
		# print(type(json))
		# if json.has(key):
		if key in json:
			gameConfig[key] = json[key] 

	# print("=====================")
	# print("gameConfig = ")
	# print(gameConfig)
	FileUtils.writeJsonToFile(os.path.join(projectPath,"settings/wechatgame.json"),gameConfig)
	pass

#压缩图片 最后测试
def pressPng():
	if(not json["isPressPng"]):
		return

	# for key in json:
	# 	print(str(key))
	# pass
	os.chdir(os.path.join(projectPath,"Framework"))
	systemRun("CALL ___a____.bat")

#容易验证
def build():
	systemRun("CocosCreator.exe --path "+projectPath+" --build")


def removeRemoteDir():
	print("############ removeRemoteDir ############")
	FileUtils.tyrRmDir(os.path.join(projectPath,"build/remote"))
	pass


def moveRemoteDir():
	print("############ moveRemoteDir ############")
	name = time.strftime("WX_%m_%d_%H_%M_%S", time.localtime()) 
	path = os.path.join(projectPath,"build",name)
	os.makedirs(path)
	FileUtils.move(os.path.join(projectPath,"build/wechatGame/remote"),path)
	pass

if __name__ == "__main__":
	# 压缩纹理
	pressPng()
	#同步构建配置表
	syncBuilder()
	# #构建 CocosCreator.exe --path D:\projectXH\creator2d\BanZhuanH5\client2 --build
	build()
	#分离引擎代码
	splitMainCode()
	# #加入打点代买
	addUmeng()

	#####删除外面的remote文件夹
	###### removeRemoteDir()
	#把里面的remote文件夹移动到外面
	moveRemoteDir()
	print("----------------end")
	os.system("pause")
	pass



